/*
 * LDIR
 * 
 * LDIR, a GPS cartography application built for Let's Do It Romania eco 
 * campaign.
 * Copyright (C) 2010 Genera Software <office@generasoftware.com>
 * 
 * http://generasoftware.com/
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include <aknviewappui.h>
#include <eikappui.h>
#include <LDIR.rsg>
#include "LDIRSplash.h"
#include "LDIRSplashView.h"
#include "events.h"
#include "Settings.h"
#include "FadeProvider.h"
#include "ImageFadeControl.h"
#include "ImageUtils.h"
#include "LDIR.hrh"

/**
 * First phase of Symbian two-phase construction. Should not 
 * contain any code that could leave.
 */
CLDIRSplash::CLDIRSplash()
	{
	}
/** 
 * Destroy child controls.
 */
CLDIRSplash::~CLDIRSplash()
	{
	delete iImageControl;
	delete iFader;
	delete iBitmap;
	}

/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is not left on cleanup stack.
 * @param aRect bounding rectangle
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return initialized instance of CLDIRSplash
 */
CLDIRSplash* CLDIRSplash::NewL( const TRect& aRect, const CCoeControl* aParent,
	MEikCommandObserver* aCommandObserver )
	{
	CLDIRSplash* self = CLDIRSplash::NewLC( aRect, aParent, aCommandObserver );
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is left on cleanup stack.
 * @param aRect The rectangle for this window
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return new instance of CLDIRSplash
 */
CLDIRSplash* CLDIRSplash::NewLC( const TRect& aRect,
	const CCoeControl* aParent, MEikCommandObserver* aCommandObserver )
	{
	CLDIRSplash* self = new ( ELeave ) CLDIRSplash();
	CleanupStack::PushL( self );
	self->ConstructL( aRect, aParent, aCommandObserver );
	return self;
	}

/**
 * Construct the control (second phase).
 *  Creates a window to contain the controls and activates it.
 * @param aRect bounding rectangle
 * @param aCommandObserver command observer
 * @param aParent owning parent, or NULL
 */
void CLDIRSplash::ConstructL( const TRect& aRect, const CCoeControl* aParent,
	MEikCommandObserver* aCommandObserver )
	{
	if ( aParent == NULL )
		{
		CreateWindowL();
		}
	else
		{
		SetContainerWindowL( *aParent );
		}
	iFocusControl = NULL;
	iCommandObserver = aCommandObserver;
	InitializeControlsL();
	SetRect( aRect );
	iBitmap = ImageUtils::LoadImageL( CSettings::InstanceL().SplashFile() );
	iFader = CFadeProvider::NewL();
	TRect imageControlRect = iBitmap->SizeInPixels();
	imageControlRect.Move( ( aRect.Width() - imageControlRect.Width() ) / 2,
		( aRect.Height() - imageControlRect.Height() ) / 2 );
	iImageControl = CImageFadeControl::NewL( imageControlRect, iFader );
	iImageControl->SetContainerWindowL( *this );
	iImageControl->SetImage( iBitmap );
	iImageControl->StartFade( CImageFadeControl::EFadeIn
		| CImageFadeControl::EFadeOut );
	iImageControl->FadeCompleted
		+= MAKEMETHODEVENTHANDLER0(void, CLDIRSplash, HandleFadeCompleted, this);
	ActivateL();
	}

/**
 * Return the number of controls in the container (override)
 * @return count
 */
TInt CLDIRSplash::CountComponentControls() const
	{
	return ( int ) ELastControl;
	}

/**
 * Get the control with the given index (override)
 * @param aIndex Control index [0...n) (limited by #CountComponentControls)
 * @return Pointer to control
 */
CCoeControl* CLDIRSplash::ComponentControl( TInt aIndex ) const
	{
	switch ( aIndex )
		{
		case EFadeImageControl:
			return iImageControl;
		}

	return NULL;
	}

/**
 *    Handle resizing of the container. This implementation will lay out
 *  full-sized controls like list boxes for any screen size, and will layout
 *  labels, editors, etc. to the size they were given in the UI designer.
 *  This code will need to be modified to adjust arbitrary controls to
 *  any screen size.
 */
void CLDIRSplash::SizeChanged()
	{
	CCoeControl::SizeChanged();
	LayoutControls();
	}

// [[[ begin generated function: do not modify
/**
 * Layout components as specified in the UI Designer
 */
void CLDIRSplash::LayoutControls()
	{
	}

/**
 *    Handle key events.
 */
TKeyResponse CLDIRSplash::OfferKeyEventL( const TKeyEvent& aKeyEvent,
	TEventCode aType )
	{
	if ( iFocusControl != NULL && iFocusControl->OfferKeyEventL( aKeyEvent,
		aType ) == EKeyWasConsumed )
		{
		return EKeyWasConsumed;
		}
	return CCoeControl::OfferKeyEventL( aKeyEvent, aType );
	}

/**
 *    Initialize each control upon creation.
 */
void CLDIRSplash::InitializeControlsL()
	{

	}

/** 
 * Handle global resource changes, such as scalable UI or skin events (override)
 */
void CLDIRSplash::HandleResourceChange( TInt aType )
	{
	CCoeControl::HandleResourceChange( aType );
	SetRect(
		iAvkonViewAppUi->View( TUid::Uid( ELDIRSplashViewId ) )->ClientRect() );
	}

/**
 *    Draw container contents.
 */
void CLDIRSplash::Draw( const TRect& aRect ) const
	{
	CWindowGc& gc = SystemGc();
	gc.Clear( aRect );
	}

//////////////////////////////////////////////////////////////////////////
void CLDIRSplash::HandleFadeCompleted()
	{
	iCommandObserver->ProcessCommandL( ECmdSplashCompleted );
	}

